home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / BZR_WRT.C < prev    next >
C/C++ Source or Header  |  1991-05-18  |  5KB  |  167 lines

  1. /******************************************************************************
  2. * Bzr-Wrt.c - Bezier handling routines - write to file.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <stdlib.h>
  9. #endif /* __MSDOS__ */
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "cagd_loc.h"
  15.  
  16. /******************************************************************************
  17. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  18. * otherwise.                                      *
  19. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  20. ******************************************************************************/
  21. int BzrCrvWriteToFile(CagdCrvStruct *Crvs, char *FileName, int Indent,
  22.                          char *Comment,    char **ErrStr)
  23. {
  24.     int i;
  25.     FILE *f;
  26.  
  27.     if ((f = fopen(FileName, "w")) == NULL) {
  28.     *ErrStr = "Fail to open file";
  29.     return FALSE;
  30.     }
  31.     i = BzrCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  32.  
  33.     fclose(f);
  34.  
  35.     return i;
  36. }
  37.  
  38. /******************************************************************************
  39. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  40. * otherwise. The file is not closed.                          *
  41. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  42. ******************************************************************************/
  43. int BzrCrvWriteToFile2(CagdCrvStruct *Crvs, FILE *f, int Indent, char *Comment,
  44.                                 char **ErrStr)
  45. {
  46.     int i, j, MaxCoord;
  47.  
  48.     if (Comment != NULL) {
  49.     _CagdFprintf(f, Indent, "#\n");
  50.     _CagdFprintf(f, Indent, "# cagd_lib - bezier curve(s) dump.\n");
  51.     _CagdFprintf(f, Indent, "#\n");
  52.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  53.     _CagdFprintf(f, Indent, "#\n");
  54.     }
  55.  
  56.     *ErrStr = NULL;
  57.  
  58.     while (Crvs) {
  59.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crvs -> PType);
  60.  
  61.     if (Crvs -> GType != CAGD_CBEZIER_TYPE) {
  62.         *ErrStr = "Given curve(s) is (are) not BEZIER curve(s)";
  63.         break;
  64.     }
  65.     _CagdFprintf(f, Indent, "[CURVE BEZIER %d %c%c\n",
  66.         Crvs -> Length,
  67.         CAGD_IS_RATIONAL_PT(Crvs -> PType) ? 'P' : 'E',
  68.         MaxCoord + '0');
  69.     Indent += 4;
  70.  
  71.     for (i = 0; i < Crvs -> Length; i++) {
  72.         _CagdFprintf(f, Indent, "[");
  73.         if (CAGD_IS_RATIONAL_PT(Crvs -> PType))
  74.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Crvs -> Points[0][i]));
  75.         for (j = 1; j <= MaxCoord; j++) {
  76.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Crvs -> Points[j][i]));
  77.         if (j < MaxCoord) _CagdFprintf(f, 0, " ");
  78.         }
  79.         _CagdFprintf(f, 0, "]\n");
  80.     }
  81.  
  82.     Indent -= 4;
  83.     _CagdFprintf(f, Indent, "]\n\n");
  84.  
  85.     Crvs = Crvs -> Pnext;
  86.     }
  87.  
  88.     return *ErrStr == NULL;
  89. }
  90.  
  91. /******************************************************************************
  92. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  93. * otherwise.                                      *
  94. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  95. ******************************************************************************/
  96. int BzrSrfWriteToFile(CagdSrfStruct *Srfs, char *FileName, int Indent,
  97.                          char *Comment,    char **ErrStr)
  98. {
  99.     int i;
  100.     FILE *f;
  101.  
  102.     if ((f = fopen(FileName, "w")) == NULL) {
  103.     *ErrStr = "Fail to open file";
  104.     return FALSE;
  105.     }
  106.     i = BzrSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  107.  
  108.     fclose(f);
  109.  
  110.     return i;
  111. }
  112.  
  113. /******************************************************************************
  114. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  115. * otherwise. The file is not closed.                          *
  116. ******************************************************************************/
  117. int BzrSrfWriteToFile2(CagdSrfStruct *Srfs, FILE *f, int Indent, char *Comment,
  118.                                 char **ErrStr)
  119. {
  120.     int i, j, MaxCoord;
  121.  
  122.     if (Comment != NULL) {
  123.     _CagdFprintf(f, Indent, "#\n");
  124.     _CagdFprintf(f, Indent, "# cagd_lib - bezier srf(s) dump.\n");
  125.     _CagdFprintf(f, Indent, "#\n");
  126.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  127.     _CagdFprintf(f, Indent, "#\n");
  128.     }
  129.  
  130.     *ErrStr = NULL;
  131.  
  132.     while (Srfs) {
  133.     MaxCoord = CAGD_NUM_OF_PT_COORD(Srfs -> PType);
  134.  
  135.     if (Srfs -> GType != CAGD_SBEZIER_TYPE) {
  136.         *ErrStr = "Given surface(s) is (are) not BEZIER surface(s)";
  137.         break;
  138.     }
  139.     _CagdFprintf(f, Indent, "[SURFACE BEZIER %d %d %c%c\n",
  140.         Srfs -> ULength, Srfs -> VLength,
  141.         CAGD_IS_RATIONAL_PT(Srfs -> PType) ? 'P' : 'E',
  142.         MaxCoord + '0');
  143.     Indent += 4;
  144.  
  145.     for (i = 0; i < Srfs -> VLength * Srfs -> ULength; i++) {
  146.         if (i && i % Srfs -> ULength == 0)
  147.         _CagdFprintf(f, 0, "\n");    /* Put empty lines between raws. */
  148.  
  149.         _CagdFprintf(f, Indent, "[");
  150.         if (CAGD_IS_RATIONAL_PT(Srfs -> PType))
  151.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Srfs -> Points[0][i]));
  152.         for (j = 1; j <= MaxCoord; j++) {
  153.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Srfs -> Points[j][i]));
  154.         if (j < MaxCoord) _CagdFprintf(f, 0, " ");
  155.         }
  156.         _CagdFprintf(f, 0, "]\n");
  157.     }
  158.  
  159.     Indent -= 4;
  160.     _CagdFprintf(f, Indent, "]\n\n");
  161.  
  162.     Srfs = Srfs -> Pnext;
  163.     }
  164.  
  165.     return *ErrStr == NULL;
  166. }
  167.